package com.changba.schedule.thread;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;

@Slf4j
public class VisibleThreadPool extends ThreadPoolTaskExecutor {

    protected void doLogs(String info) {
        //线程相关信息
        String threadNamePrefix = this.getThreadNamePrefix();
        long taskCount = this.getThreadPoolExecutor().getTaskCount();
        long completedTaskCount = this.getThreadPoolExecutor().getCompletedTaskCount();
        int activeCount = this.getThreadPoolExecutor().getActiveCount();
        int size = this.getThreadPoolExecutor().getQueue().size();
        log.info("线程执行方式={},线程前缀={},线程总任务数={},已完成任务数={},剩余任务数={},线程池队列大小={}",
                info,threadNamePrefix,taskCount,completedTaskCount,activeCount,size);
    }


    @Override
    public <T> Future<T> submit(Callable<T> task) {
        Future<T> submit = super.submit(task);
        doLogs("do submit");
        return submit;
    }

    @Override
    public void execute(Runnable task) {
        super.execute(task);
        doLogs("do execute");
    }
}
